/*
 * @Author: zhousg
 * @Date: 2020-11-04 14:59:38
 * @Last Modified by: zhousg
 * @Last Modified time: 2020-12-23 12:05:38
 */

export const confirmReceipt = {
  methods: {
    confirmReceipt () {
      this.$confirm({ title: '温馨提示', text: '您确认收到货吗？' }).then(async (valid) => {
        if (valid) {
          await this.$http.put(`member/order/${this.order.id}/receipt`)
          this.$message({ message: '确认收货成功', type: 'success' })
          this.order.orderState = 4
          this.$emit('on-confirm-success')
        }
      }).catch(e => {
        this.$message({ message: '确认收货失败', type: 'error' })
      })
    }
  }
}

export const downTimeLogic = {
  data () {
    return {
      downTime: ''
    }
  },
  watch: {
    order: {
      handler () {
        if (this.order.orderState === 1) {
          this.startDownTime()
        }
      },
      immediate: true
    }
  },
  methods: {
    formatTime (second) {
      const m = Math.floor(second / 60)
      const s = Math.floor(second % 60)
      this.downTime = `剩余${m}分${s}秒`
    },
    startDownTime () {
      const endTime = new Date(this.order.payLatestTime).getTime()
      const nowTime = Date.now()
      let second = Math.ceil((endTime - nowTime) / 1000)
      if (second < 0) return
      this.formatTime(second)
      this.timer = window.setInterval(() => {
        second--
        this.formatTime(second)
        if (second <= 0) {
          clearInterval(this.timer)
          this.downTime = ''
        }
      }, 1000)
    }
  }
}
